<?php
namespace app\controller\admin;

use app\BaseController;
use app\controller\AdminController;
use app\middleware\AdminAuth;
use app\middleware\UniAccount;
use app\model\User as userModel;

use Firebase\JWT\JWT;
use think\cache\driver\Redis;
use think\facade\Request;
use think\Request as Request2;
use think\exception\ValidateException;
// class User extends BaseController
class User extends AdminController
{


    // protected $middleware  = [
        // AdminAuth::class    => [
        //     'except' 	=> [
        //         'login',
        //     ]
        // ], 
        // UniAccount::class    => [
        //     'except' 	=> [
        //         'login',
        //     ]
        // ],
    // ];
    public function index()
    {
        return json( [


            'status'    => 0,
            'message'   => 'index 页面'
        ] );
    }

    public function info()
    {
        $user = userModel::find(1);
        return json( $user);
        
        return json( [

            'status'    => 0,
            'message'   => '用户信息..'
        ] );
    }

    public function check( Request $request ){

        $token = Request::param('token');

        try {

            $ret = userModel::checkToken( $token );

        }catch (\Exception $e) {
            
            return json($e->getMessage());
        }
        
        return json( $ret );
    }

    // 用来检测是否为微擎环境， 若是 创建登陆用的token，并返回 
    public function wq_check( Request $request ){

        $wq_user_id = Request::param('user_id');
        $wq_uniacid = Request::param('uniacid');

        $ret = [
            'code'=>0,
        ];
        $wq_user_id = \is_numeric( $wq_user_id );
        $wq_uniacid = \is_numeric( $wq_uniacid );

        if( empty( $wq_user_id ) || empty( $wq_uniacid ) ){
            $ret['code'] = 1;
            $ret['message'] = '微擎环境不合法';
            return json( $ret );
        }

        try {
            
            // todo 先检测是否为微擎环境
            
            // todo 创建token
            $ret['token'] = userModel::createToken( $wq_user_id, $wq_uniacid );

        }catch (\Exception $e) {
            
            return json($e->getMessage());
        }
        
        return json( $ret );
    }

    public function login( Request $request )
    {
        
        // return Request::param('name');
        // return $request->param('name');
        $username = Request::param('name');
        $password = Request::param('password');

        


        try {

            // 这里是主体代码
            $ret = userModel::login( $username, $password );
            return json( $ret );

        } catch (ValidateException $e) {
            // 这是进行验证异常捕获
            return json($e->getError());
        } catch (\Exception $e) {
            // 这是进行异常捕获
            return json($e->getMessage());
        }
        
    }

    /**
     * 退出登录
     */
    public function logout( Request $request ){

        $token = Request::param('token');
        //todo 删除redis中的token 
        $ret = userModel::logout( $token );

        return json( $ret );
    }

    public function changepwd( Request2 $request ){

        $auth = $request->auth;
        // $auth = Request::auth;
        $current_password = Request::param('current_password');
        $new_password = Request::param('new_password');

        if( empty($current_password) ){

            return [
                'code'      => 1,
                'message'   => '原始密码不能为空',
            ];
        }

        
        // if( empty($new_password) ){

        //     return [
        //         'code'      => 1,
        //         'message'   => '原始密码不能为空',
        //     ];
        // }
        $pwd_length=strlen( $new_password ); 
        if( $pwd_length < 6 ){

            return [
                'code'      => 1,
                'message'   => '新密码长度不小于6位数',
            ];
        }

        $ret = userModel::changePassword( $auth['user_id'], $current_password, $new_password );

        return json( $ret );
    }

}